//
//  KMBaseTableRootController.m
//  BaseProject
//
//  Created by 陈家将 on 16/9/24.
//  Copyright © 2016年 szteyou. All rights reserved.
//

#import "KMBaseTableRootController.h"
#import "KMListHeaderView.h"
#import "KMSuperHeaderView.h"
#import "KMListHeaderView.h"
#import "KMSegmentedHeaderView.h"
#import "KMTableListConfig.h"

@interface KMBaseTableRootController ()<UIScrollViewDelegate>

/** 页面请求标识*/
@property (strong, nonatomic) UIViewController *requestVc;
@property (strong, nonatomic) KMSuperHeaderView *listHeaderView;
@property (strong, nonatomic) UIScrollView *listContenView;

@end

@implementation KMBaseTableRootController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)dealloc {
    for (id vc in self.childViewControllers) {
        [self removeObserver:vc forKeyPath:@"requestVc"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Private Methods
- (void)selectedPageAtIndex:(NSInteger)index {
    
#ifdef DEBUG
    if (index >= self.childViewControllers.count) {
        [self alertWithSytle:UIAlertControllerStyleAlert title:@"提示" message:@"设置的下标越界!\n无法跳转指定页面，抱歉!" cancleTitle:@"好的"];
    }
#endif
    
    index = (index >= self.childViewControllers.count ? 0 : index);
    self.listHeaderView.selectedIndex = index;
    self.requestVc = self.childViewControllers[index];
    [self.listContenView setContentOffset:(CGPoint){SCREEN_WIDTH * index, 0} animated:NO];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat proportion = scrollView.contentOffset.x / scrollView.contentSize.width;
    self.listHeaderView.sliderView.layer.transform = CATransform3DMakeTranslation(proportion * SCREEN_WIDTH, 0, 0);
    NSInteger index = (int)(scrollView.contentOffset.x / SCREEN_WIDTH + 0.5);
    self.listHeaderView.selectedIndex = index;
    self.requestVc = self.childViewControllers[index];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//    NSInteger index = (int)(scrollView.contentOffset.x / SCREEN_WIDTH + 0.5);
//    self.listHeaderView.selectedIndex = index;
    NSInteger index = self.listHeaderView.selectedIndex;
    DBLog(@"%ld",index);
    self.requestVc = self.childViewControllers[index];
}

#pragma mark - Getter And Setter Methods

- (void)setViewControllers:(NSArray *)viewControllers {
    _viewControllers = viewControllers;
    [_viewControllers enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self addChildViewController:obj];
    }];
    
    [self.view addSubview:self.listContenView];
    
    NSInteger tempIndex = 0;
    for (BaseViewController *vc in self.childViewControllers) {
        [vc.view willMoveToSuperview:self.view];
        vc.view.frame = CGRectMake(tempIndex * SCREEN_WIDTH, 0, SCREEN_WIDTH, self.listContenView.bounds.size.height);
        [self.listContenView addSubview:vc.view];
        [vc.view didMoveToSuperview];
        
        // KVO监听请求信号
        [self addObserver:vc forKeyPath:@"requestVc" options:NSKeyValueObservingOptionNew context:nil];
        tempIndex++;
    }
    
    if (self.childViewControllers.count) {
        [self selectedPageAtIndex:_selectedIndex]; // 设置默认选中页
    }
}

- (void)setViewControllerNames:(NSArray *)viewControllerNames {
    
    switch (self.headerViewType) {
            
        case KMHeaderTypeList:
            _listHeaderView = [[KMListHeaderView alloc] initWithFrame:(CGRect){0, 0, SCREEN_WIDTH, KMListHeaderHeight}];
            break;
        case KMHeaderTypeSegmented:
            
            _listHeaderView = [[KMSegmentedHeaderView alloc] initWithFrame:(CGRect){0, 0, SCREEN_WIDTH, KMListHeaderHeight}];
            break;
            
        default:
            break;
    }
    
    _viewControllerNames = viewControllerNames;
    _listHeaderView.titles = _viewControllerNames;
    
    // Actions
    WS(weakSelf)
    [_listHeaderView selectedItemCompletion:^(NSInteger index) {
        [weakSelf.listContenView setContentOffset:CGPointMake(SCREEN_WIDTH * index, 0) animated:NO];
        weakSelf.requestVc = weakSelf.childViewControllers[index];
    }];
    
    [self.view addSubview:_listHeaderView];
    
    if (self.childViewControllers.count) {
        [self selectedPageAtIndex:_selectedIndex]; // 设置默认选中页
    }
}

- (UIScrollView *)listContenView {
    if (_listContenView == nil) {
        CGFloat magin = KMListHeaderHeight;
        _listContenView = [[UIScrollView alloc] initWithFrame:SCREEN_BOUNDS_WITHOUT_NAV_AND_TOP_MARGIN(magin)];
        _listContenView.delegate = self;
        _listContenView.pagingEnabled = YES;
        _listContenView.bounces = NO;
        _listContenView.showsHorizontalScrollIndicator = NO;
        _listContenView.contentSize = CGSizeMake(SCREEN_WIDTH * self.childViewControllers.count, 0);
    }
    
    return _listContenView;
}

- (void)setSelectedIndex:(NSInteger)selectedIndex {
    _selectedIndex = selectedIndex;
    
    if (self.viewControllers.count) {
        [self selectedPageAtIndex:selectedIndex];
    }
}

- (void)setLiteHeaderHidden:(BOOL)liteHeaderHidden {
    _liteHeaderHidden = liteHeaderHidden;
    self.listHeaderView.bottomView.hidden = liteHeaderHidden;
}

@end
